package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.common.BaseAction;
@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class AreaAction extends BaseAction<Area>{

	//注入业务层对象
	@Autowired
	private AreaService areaService;
	private File file;
	public void setFile(File file) {
		this.file = file;
	}

	@Action(value="area_batchImport")
	public String batchImport() throws IOException{
		//加载.xls文件
		List<Area> list=new ArrayList<Area>();
		//加载.xml文件对象
		HSSFWorkbook hssfWorkbook=new HSSFWorkbook(new FileInputStream(file));
		//读取第一个sheet
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		
		//遍历读取sheet中的每一行
		for (Row row : sheet) {
			//一行数据,对应一个区域对象
			//判断是不是第一行,不是就挑走
			if(row.getRowNum()==0){
				continue;
			}
			//判断是不是最后一行
			if(row.getCell(0)==null ||
					StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;
			}
			Area area=new Area();
			//id provice city distinct postcode
			area.setId(row.getCell(0).getStringCellValue());;
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			String province = row.getCell(1).getStringCellValue();
			String city = row.getCell(2).getStringCellValue();
			String district = row.getCell(3).getStringCellValue();
			province=province.substring(0, province.length()-1);
			city=city.substring(0, city.length()-1);
			district=district.substring(0, district.length()-1);
			//简称  [B,J,B,J,X,L]
			String[] headArray = PinYin4jUtils.getHeadByString(province+city+district);
			//citycode   shorycode
			StringBuffer sb=new StringBuffer();
			for (String head : headArray) {
				sb.append(head);
			}
			String headType = sb.toString();
			area.setShortcode(headType);
			
			//citycode
			String citycode = PinYin4jUtils.hanziToPinyin(city, "");
			area.setCitycode(citycode);
			list.add(area);
		}
		//调用业务层 saveBatch
		areaService.saveBatch(list);
		return NONE;
	}
	
	
	

	//调用area_pageQuery方法执行有条件查询定区
	@Action(value="area_pageQuery",results={
			@Result(name="success",type="json")
	})
	
	public String pageQuery(){
		Pageable pageable=new PageRequest(page-1,rows);
		//构建条件查询对象
		Specification<Area> specification =new Specification<Area>() {
			@Override
			public Predicate toPredicate(Root<Area> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list=new ArrayList<Predicate>();
				//添加条件,省的判断
				if(StringUtils.isNotBlank(model.getProvince())){
					Predicate p1 = cb.like(root.get("province").as(String.class),
							"%"+model.getProvince()+"%");
					list.add(p1);
				}
//				市的判断
				if(StringUtils.isNotBlank(model.getCity())){
					Predicate p2 = cb.like(root.get("city").as(String.class),
							"%"+model.getCity()+"%");
					list.add(p2);
				}
				//街道的判断
				if(StringUtils.isNotBlank(model.getDistrict())){
					Predicate p3 = cb.like(root.get("district").as(String.class),
							"%"+model.getDistrict()+"%");
					list.add(p3);
				}
				return 	cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Area> page=areaService.findAll(specification,pageable);
		
		pushToValueStack(page);
		return SUCCESS;
	}
	
	

}
